<template>
  <div class="page-box">
    <!-- 筛选区域 -->
    <div v-show="flag" class="search-box">
      <!-- :inline="true" 表单在一行显示 -->
      <el-form size="mini" :inline="true" class="demo-form-inline">
        <el-form-item label="角色名">
          <el-input v-model="params.name" placeholder="请输入" />
        </el-form-item>
        <el-form-item>
          <el-button icon="el-icon-search" type="primary" @click="search">搜索</el-button>
          <el-button icon="el-icon-refresh" @click="reload">重置</el-button>
        </el-form-item>
      </el-form>
    </div>
    <!-- 工具条区域 -->
    <div class="tabbar-box">
      <el-row type="flex" justify="space-between">
        <div>
          <el-button plain type="primary" icon="el-icon-plus" size="mini" @click="getdetail('add')">新增</el-button>
          <el-button plain type="warning" icon="el-icon-download" size="mini" @click="exportExcel">导出</el-button>
        </div>
        <div>
          <el-tooltip class="item" effect="dark" content="关闭搜索区" placement="top">
            <el-button :icon="up" size="small" circle @click="hide" />
          </el-tooltip>
          <el-tooltip class="item" effect="dark" content="刷新" placement="top">
            <el-button icon="el-icon-refresh" size="small" circle @click="reload" />
          </el-tooltip>
          <el-tooltip class="item" effect="dark" content="筛选" placement="top-start">
            <el-popover placement="bottom" width="100" trigger="click">
              <el-checkbox-group v-model="checkList">
                <el-checkbox class="fa fa fa-dedent" label="角色ID" />
                <el-checkbox class="fa fa fa-dedent" label="角色名称" disabled />
                <el-checkbox class="fa fa fa-dedent" label="角色描述" />
                <el-checkbox class="fa fa fa-dedent" label="创建时间" />
              </el-checkbox-group>
              <el-button slot="reference" class="filter" icon="el-icon-menu" size="small" circle />
            </el-popover>
          </el-tooltip>
        </div>
      </el-row>
    </div>
    <!-- 表格区域 -->
    <div class="table-box">
      <el-table id="outTable" ref="myTable" :data="rolelist" style="width: 100%" border>
        <el-table-column type="index" label="#" width="50" />
        <el-table-column v-if="checkList.includes('角色ID')" prop="role_id" label="角色ID" />
        <el-table-column v-if="checkList.includes('角色名称')" prop="name" label="角色名称" />
        <el-table-column v-if="checkList.includes('角色描述')" prop="description" label="角色描述" />
        <el-table-column
          v-if="checkList.includes('创建时间')"
          prop="create_date"
          label="创建时间"
        />
        <el-table-column prop="" label="操作">
          <template #default="{row}">
            <el-button size="mini" circle type="primary" icon="el-icon-edit" @click="addrole(row.id)" />
            <el-button size="mini" type="danger" icon="el-icon-delete" circle />
          </template>
        </el-table-column>
      </el-table>
    </div>
    <el-dialog ref="tagdialog" :title="dialog" :visible.sync="dialogVisible" width="45%" :before-close="handleClose">
      <el-form ref="roleform" :model="roleform" label-width="100px" class="demo-dynamic" size="medium">
        <el-form-item prop="name" label="角色名称" :rules="{ required: true, message: '请输入角色名称', trigger: 'blur' }">
          <el-row>
            <el-col :span="15"> <el-input v-model="roleform.name" /></el-col>
          </el-row>
        </el-form-item>
        <el-form-item prop="description" label="角色描述" :rules="{ required: true, message: '请输入角色描述', trigger: 'blur' }">
          <el-row>
            <el-col :span="15"><el-input v-model="roleform.description" type="textarea" /></el-col>
          </el-row>
        </el-form-item>
        <el-form-item>
          <el-tree
            v-if="showtree"
            ref="tree"
            :data="roledata"
            show-checkbox
            node-key="id"
            highlight-current
            :default-expanded-keys="defaultExpandedKeys"
            :props="{ label: 'title', children: 'children' }"
          />
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button size="small" @click="handleClose">取 消</el-button>
        <el-button size="small" type="primary">确 定</el-button>
      </span>
    </el-dialog>
    <!-- 分页区域 -->
    <div class="page">
      <el-pagination
        v-if="isTableAlive"
        background
        :current-page="params.page"
        :page-sizes="[10, 20, 30, 40]"
        :page-size="params.limit"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
      />
    </div>
  </div>
</template>
<script>
function getarr(arr) {
  const arr1 = []
  arr.forEach(item => {
    arr1.push(item.id)
    if (item.children) {
      arr1.push(...getarr(item.children))
    }
  })
  return arr1
}
import { getrole, getroledetail, getmenu } from '@/api/power'
import { exportTableAsXLSX } from '@/utils/unitools'
export default {
  data() {
    return {
      dialog: '添加角色',
      dialogVisible: false,
      defaultExpandedKeys: [],
      roledata: [],
      showtree: true,
      roleform: {
        description: '',
        name: ''
      },
      checkList: [
        '角色ID',
        '角色名称',
        '角色描述',
        '创建时间'],
      rolelist: [],
      params: {
        name: '',
        page: 1,
        limit: 10
      },
      flag: true,
      total: 0,
      up: 'el-icon-arrow-up',
      isTableAlive: true
    }
  },
  created() {
    this.getrolelist()
    this.getmenulist()
  },
  methods: {
    async addrole(id) {
      this.showtree = true
      if (id) {
        this.dialog = '修改角色'
        const res = await getroledetail(id)
        // this.getmenulist()
        // console.log(res)
        this.roleform = res
        // console.log(res.authList)
        this.dialogVisible = true
        const finallyarr = getarr(res.authList)
        console.log(finallyarr)
        // const arr = res.authList.map(item => {
        //   if (item.children) {
        //     return item.children.map(item => item.id)
        //   }
        // })
        // const arr1 = res.authList.map(item => item.children.map(item => {
        //   if (item.children) {
        //     return item.children.map(item => item.id)
        //   }
        // }))
        // console.log(arr1.flat(Infinity))
        // console.log([...arr[0]])
        this.$nextTick(() => {
          this.$refs.tree.setCheckedNodes(res.authList)
          this.defaultExpandedKeys = finallyarr
          console.log(this.defaultExpandedKeys)
        })
      }
    },
    exportExcel() {
      exportTableAsXLSX('outTable')
    },
    handleSizeChange(val) {
      // console.log(`每页 ${val} 条`)
      this.params.page = 1
      this.params.limit = val
      this.getrolelist()
    },
    handleCurrentChange(val) {
      this.params.page = val
      this.getrolelist()
    },
    handleClose() {
      // this.roledata = []
      this.showtree = false
      this.defaultExpandedKeys = []
      this.dialogVisible = false
    },
    async getmenulist() {
      const res = await getmenu()
      console.log(res)
      this.roledata = res
    },
    async getrolelist() {
      const res = await getrole(this.params)
      console.log(res)
      this.rolelist = res.data
      this.total = res.count
    },
    hide() {
      this.flag = !this.flag
      this.up = this.flag ? 'el-icon-arrow-up' : 'el-icon-arrow-down'
    },
    reload() {
      this.rolelist = []
      this.params = {
        name: '',
        page: 1,
        limit: 10
      }
      this.isTableAlive = false
      setTimeout(() => {
        this.isTableAlive = true
      }, 100)
      this.getrolelist()
    },
    search() {
      this.getrolelist()
    }

  }
}
</script>
<style lang="scss" scoped>
.page-box {
  padding: 20px;
  .search-box,
  .tabbar-box {
    padding: 10px 10px;
    height: 50px;
    .filter {
      margin-left: 10px;
    }
  }
  .fontsome {
    margin-left: 15px;
    &:hover {
      border-bottom: 1px solid #51a7ff;
      padding-bottom: 5px;
      color: #51a7ff;
    }
  }
}
</style>
